package com.chenglai.seckill.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.seckill.pojo.SeckillGoods;
import com.chenglai.seckill.pojo.SeckillInfo;
import com.chenglai.seckill.service.SeckillGoodsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/23 15:18
 */
@RestController
@RequestMapping("/seckillGoods")
public class SeckillGoodsController {

    @Resource
    private SeckillGoodsService seckillGoodsService;

    //秒杀活动开始前预热
    @PostMapping("/getSeckillGoodsAllList")
    @ApiOperation(value = "获取秒杀活动商品信息")
    public Result getSeckillGoodsAllList(@ApiParam(name = "seckillId", value = "秒杀活动id") @RequestParam("seckillId") Integer seckillId) {
        try {
            List<SeckillGoods> seckillGoods = seckillGoodsService.getSeckillGoodsAllList(seckillId);
            return new Result(true, StatusCode.OK, "操作成功", seckillGoods);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/editSeckillGoods")
    @ApiOperation(value = "添加/修改秒杀商品")
    public Result editSeckillGoods(SeckillGoods seckillGoods) {
        try {
            Map map = seckillGoodsService.editSeckillGoods(seckillGoods);
            return new Result(true, StatusCode.OK, "操作成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/detail")
    @ApiOperation(value = "秒杀商品详情")
    public Result categoryDetail(@ApiParam(name = "sgId", value = "主键ID") Integer sgId) {
        try {
            SeckillGoods detail = seckillGoodsService.detail(sgId);
            return new Result(true, StatusCode.OK, "查询成功", detail);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

}
